package com.zxy.supplier_system.server.entity

import jakarta.persistence.*
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.math.BigDecimal
import java.time.OffsetDateTime

@Entity
@EntityListeners(AuditingEntityListener::class)
@DynamicUpdate
@DynamicInsert
class DirectSaleOrder {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id:Int = 0

    @CreatedBy
    @ManyToOne(optional = false)
    @JoinColumn
    lateinit var createdBy:Employee

    @ManyToOne(optional = false)
    @JoinColumn
    lateinit var client: DirectSaleClient

    @CreatedDate
    lateinit var createdDateTime: OffsetDateTime

    @OneToMany(cascade = [CascadeType.ALL], mappedBy = "order", orphanRemoval = true)
    var items:MutableList<DirectSaleOrderItem> = mutableListOf()

    var submittedDateTime: OffsetDateTime? = null

    @OneToMany(cascade = [CascadeType.ALL], mappedBy = "order", orphanRemoval = true)
    var paidRecords: MutableList<DirectSaleOrderPayRecord> = mutableListOf()

    @get:Transient
    val paidPrice:BigDecimal
        get() {
            return this.paidRecords.sumOf { it.price }
        }

    @get:Transient
    val shouldPaidPrice:BigDecimal
        get() {
            return this.items.sumOf { it.price }
        }


}
